﻿using Ekton_Manage_DomainCore.Aggregate;
using Ekton_Manage_IBusiness;
using Ekton_Manage_InfrastructureCore.Common;
using Ekton_Manage_InfrastructureCore.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using static Ekton_Manage_DTO.CommonModels.DtoFileModel;

namespace Ekton_Manage_Web.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize("Permission")]
    public class OrderListController : ControllerBase
    {
        private readonly IOrderList _orderList;
        private readonly IBaseRepository<user_orderView, int> _userOrderrepository;

        public OrderListController(IOrderList orderList, IBaseRepository<user_orderView, int> userOrderrepository)
        {
            _orderList = orderList;
            _userOrderrepository = userOrderrepository;
        }
        /// <summary>
        /// 查询全部订单
        /// </summary>
        /// 
        [HttpGet]
        public async Task<MessageModel<PageModel<user_orderView>>> GetOrderList([FromQuery] QueryDataParame query)
        {
            var result = await _orderList.SelectOrderList(query);
            return new MessageModel<PageModel<user_orderView>>
            {
                msg = "数据获取成功",
                response = new PageModel<user_orderView>
                {
                    pageIndex = query.pageIndex,
                    pageCount = Convert.ToInt32(Math.Ceiling(Convert.ToDecimal(result.Item2) / query.pageSize)),
                    dataCount = result.Item2,
                    pageSize = query.pageSize,
                    data = result.Item1
                }
            };
        }

        /// <summary>
        /// 导出订单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<FileResult> ExportData([FromBody] ExportFormData formData)
        {
            List<user_orderView> data = new();
            if (formData.list.Count < 1)
                data = await _userOrderrepository.QueryTableList(m => true);
            else
                data = await _userOrderrepository.QueryTableList(m => formData.list.Contains(m.id));
            //创建一个新的excel文件
            HSSFWorkbook book = new HSSFWorkbook();
            //创建一个工作区
            ISheet sheet = book.CreateSheet("sheet1");
            //创建一行
            IRow row = sheet.CreateRow(0);
            for (int i = 0; i < 5; i++)
            {
                sheet.SetColumnWidth(i, 23 * 156);
            }
            sheet.SetColumnWidth(4, 30 * 156);
            sheet.SetColumnWidth(1, 21 * 156);
            //定义一个样式
            ICellStyle setborder = book.CreateCellStyle();
            setborder.BorderLeft = BorderStyle.Thin;
            setborder.BorderRight = BorderStyle.Thin;
            setborder.BorderBottom = BorderStyle.Thin;
            setborder.BorderTop = BorderStyle.Thin;

            //文字水平和垂直对齐方式
            setborder.VerticalAlignment = VerticalAlignment.Center;//垂直居中
            setborder.Alignment = HorizontalAlignment.Center;//水平居中
            setborder.WrapText = true;//自动换行

            //再定义一个样式，用来设置最上面标题行的样式
            ICellStyle setborderdeth = book.CreateCellStyle();
            setborderdeth.BorderLeft = BorderStyle.Thin;
            setborderdeth.BorderRight = BorderStyle.Thin;
            setborderdeth.BorderBottom = BorderStyle.Thin;
            setborderdeth.BorderTop = BorderStyle.Thin;

            //定义一个字体样式
            IFont font = book.CreateFont();
            //将字体设为红色
            font.Color = IndexedColors.Red.Index;
            setborderdeth.SetFont(font);
            setborderdeth.VerticalAlignment = VerticalAlignment.Center;
            setborderdeth.Alignment = HorizontalAlignment.Center;
            setborderdeth.WrapText = true;

            row.HeightInPoints = 25;
            //设置单元格的值
            row.CreateCell(0).SetCellValue("序号");
            row.GetCell(0).CellStyle = setborderdeth;
            row.CreateCell(1).SetCellValue("订单号");
            row.GetCell(1).CellStyle = setborderdeth;
            row.CreateCell(2).SetCellValue("所属公司组织");
            row.GetCell(2).CellStyle = setborderdeth;
            row.CreateCell(3).SetCellValue("用户姓名");
            row.GetCell(3).CellStyle = setborderdeth;
            row.CreateCell(4).SetCellValue("订单状态");
            row.GetCell(4).CellStyle = setborderdeth;
            row.CreateCell(5).SetCellValue("下单时间");
            row.GetCell(5).CellStyle = setborderdeth;
            for (int i = 0; i < data.Count; i++)
            {
                IRow row1 = sheet.CreateRow(i + 1);
                row1.HeightInPoints = 21;
                row1.CreateCell(0).SetCellValue(i);
                row1.GetCell(0).CellStyle = setborder;
                row1.CreateCell(1).SetCellValue(data[i].order_id);
                row1.GetCell(1).CellStyle = setborder;
                row1.CreateCell(2).SetCellValue(Convert.ToString(data[i].company));
                row1.GetCell(2).CellStyle = setborder;
                row1.CreateCell(3).SetCellValue(Convert.ToString(data[i].name));
                row1.GetCell(3).CellStyle = setborder;
                row1.CreateCell(4).SetCellValue(Convert.ToString(data[i].state));
                row1.GetCell(4).CellStyle = setborder;
                row1.CreateCell(5).SetCellValue(Convert.ToString(data[i].order_time));
                row1.GetCell(5).CellStyle = setborder;
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            DateTime dttime = DateTime.Now;
            string datetime = dttime.ToString("yyyy-MM-dd");
            string filename = "订单列表导出" + datetime + ".xls";
            return File(ms, "application/vns.ms-excel", filename);
        }

        /// <summary>
        /// 查询订单的数量
        /// </summary>
        [HttpGet]
        public async Task<Message> GetOrderCount(int type)
        {
            return await _orderList.GetOrderCount(type);
        }

        /// <summary>
        /// 订单量
        /// </summary>
        [HttpGet]
        public async Task<Message> GetOrderNumber()
        {
            return await _orderList.GetOrderNumber();
        }

        /// <summary>
        /// 修改订单状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<Message> PutOrderState(int id,int state)
        {
            return await _orderList.PutOrderState(id,state);
        }
    }
}
